﻿#region Copyright ©2008-2009, Technische Universitaet Muenchen
// ====================================================================================================
//
// Last Changed by       $Author: then $
// Last Changed Date     $LastChangedDate: 2009-04-01 10:10:55 +0200 (Wed, 01 Apr 2009) $
// Last Changed Revision $Rev: 49 $
//
// ====================================================================================================
#endregion

using System.IO;
using System.Windows;
using System.Windows.Media;
using Microsoft.Win32;

using Tum.CollabXT.VModellXT.Resources;
using Tum.CollabXT.VModellXT.Properties;

namespace Tum.CollabXT.VModellXT
{
    /// <summary>
    /// Interaction logic for VModellConfigPage.xaml
    /// </summary>
    public partial class VModellConfigPage : ProcessProviderConfigPage
    {
        VModellXTProcessProvider ProcessProvider;
        IConversionWorkflow ConversionWorkflow;


        public VModellConfigPage()
        {
            InitializeComponent();
        }

        public override void ReceiveProviderHandle(IProcessProvider processProvider)
        {
            ProcessProvider = processProvider as VModellXTProcessProvider;

            if (File.Exists(Settings.Default.LastVModellPath))
                ProcessProvider.VModellFile = Settings.Default.LastVModellPath;

            SetValues();
        }

        public override void ReceiveWorkflowHandle(IConversionWorkflow workflow)
        {
            ConversionWorkflow = workflow;

            UpdateWorkflowButtons();
        }

        private void buttonSetVModellFilePath_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDlg = new OpenFileDialog();
            openFileDlg.Filter = VModellXT.Resources.Language.ConfigPage_VModellFiles + " (*.xml)|*.xml";
            openFileDlg.Multiselect = false;
            openFileDlg.RestoreDirectory = true;

            if (openFileDlg.ShowDialog() == true)
            {
                ProcessProvider.VModellFile = openFileDlg.FileName;
                ConversionWorkflow.InvalidateProcessing();
                SetValues();

                //Save path as 'last model file'
                Settings.Default.LastVModellPath = ProcessProvider.VModellFile;
                Settings.Default.Save();
            }
        }

        private void buttonProjectFilePath_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDlg = new OpenFileDialog();
            openFileDlg.Filter = VModellXT.Resources.Language.ConfigPage_VModellProjectFiles + " (*.vmp)|*.vmp";
            openFileDlg.Multiselect = false;
            openFileDlg.RestoreDirectory = true;

            if (openFileDlg.ShowDialog() == true)
            {
                ProcessProvider.ProjectFile = openFileDlg.FileName;
                ConversionWorkflow.InvalidateProcessing();
                SetValues();
            }
        }

        private void SetValues()
        {
            //V-Modell file
            if (ProcessProvider.VModellFile == null)
                labelVModellFilePath.Content = VModellXT.Resources.Language.PathNotSet;
            else
            {
                labelVModellFilePath.Content = ProcessProvider.VModellFile;
                labelVModellFilePath.Foreground = new SolidColorBrush(Color.FromRgb(0, 0, 0));
            }

            //Project file
            if (ProcessProvider.ProjectFile == null)
                labelProjectFilePath.Content = VModellXT.Resources.Language.PathNotSet;
            else
            {
                labelProjectFilePath.Content = ProcessProvider.ProjectFile;
                labelProjectFilePath.Foreground = new SolidColorBrush(Color.FromRgb(0, 0, 0));
            }

            //Button states
            UpdateWorkflowButtons();
        }

        private void UpdateWorkflowButtons()
        {
            if(ProcessProvider != null)
            {
                ConversionWorkflow.UpdateButtonState(true, ProcessProvider.VModellFile != null && ProcessProvider.ProjectFile != null);
            }
        }
    }
}
